package org.numanint.wizard;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Iterator;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.numanint.utils.UIutils;

public class WebProjectWizardTwo extends WizardPage {	
	protected WebProjectWizardTwo(String pageName) {
		super(pageName);
	}

	public WebProjectWizardTwo() {
		super("");
		setTitle("Numanint-新建一个Web框架");
		setDescription("设置Model层数据库连接配置");
		setImageDescriptor(UIutils.getImageDescriptor("icons/numanint-48.png"));
		setPageComplete(false);
	}


	public void createControl(Composite parent) {
		initializeDialogUnits(parent);
		final Composite composite = createClientArea(parent);
		paint(composite);
		setControl(composite);
		Dialog.applyDialogFont(composite);

	}

	private Composite createClientArea(Composite composite) {
		Composite composite1 = new Composite(composite, SWT.NONE);
		GridLayout gridlayout = new GridLayout();		
		gridlayout.marginWidth = 20;
		gridlayout.marginHeight = 20;
		gridlayout.numColumns = 2;
		composite1.setLayout(gridlayout);
		return composite1;
	}
	
	
	private void paint(Composite composite) {
		final HashMap<String,WebProjectDBConfig> dbSourceMap = new HashMap<String,WebProjectDBConfig>();
        dbSourceMap.put("Oracle",
        		new WebProjectDBConfig("oracle.jdbc.driver.OracleDriver",
        				"jdbc:oracle:thin:@192.168.4.201:1521:cring",
        				"mzdev",
        				"mzdev")
        		);
        dbSourceMap.put("Mysql",
        		new WebProjectDBConfig("com.mysql.jdbc.Driver",
        				"jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=GBK",
        				"root",
        				"ghost")
        		);
        WebProjectConfig.projectDBConfig = dbSourceMap.get("Oracle");
		Label labelType = new Label(composite, 64);
		labelType.setText("选择数据库类型：");

		final Combo combo = new Combo(composite,64);
		final Label labelDriver = new Label(composite, 64);
		labelDriver.setText("数据库Driver：");
		final Text textDriver = new Text(composite,SWT.SINGLE);
		final Label labelUrl = new Label(composite, 64);
		labelUrl.setText("数据库Url：");
		final Text textUrl = new Text(composite,SWT.SINGLE);
		final Label labelUsername = new Label(composite, 64);
		labelUsername.setText("数据库Username：");
		final Text textUsername = new Text(composite,SWT.SINGLE);
		final Label labelPassword = new Label(composite, 64);
		labelPassword.setText("数据库Password：");
		final Text textPassword = new Text(composite,SWT.SINGLE);
		final Button buttonTestConnection = new Button(composite,SWT.PUSH);
		final Label labelTest= new Label(composite, 64);
		labelTest.setText("");
		final Label labelSchema= new Label(composite, 64);
		labelSchema.setText("选择Schema:");
		final Combo comboSchema = new Combo(composite,SWT.DROP_DOWN | SWT.READ_ONLY);
		buttonTestConnection.setText("测试连接");
		buttonTestConnection.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {		    	 
		         try{
		        	 WebProjectConfig.projectDBConfig.driver = textDriver.getText();
		        	 WebProjectConfig.projectDBConfig.url = textUrl.getText();
		        	 WebProjectConfig.projectDBConfig.username = textUsername.getText();
		        	 WebProjectConfig.projectDBConfig.password = textPassword.getText();
		        	 Class.forName(WebProjectConfig.projectDBConfig.driver);
		        	 if(WebProjectConfig.conn!=null&&!WebProjectConfig.conn.isClosed())WebProjectConfig.conn.close();
		        	 WebProjectConfig.conn = DriverManager.getConnection( 
		        			 WebProjectConfig.projectDBConfig.url,
		        			 WebProjectConfig.projectDBConfig.username,
		        			 WebProjectConfig.projectDBConfig.password);
		        	 if(WebProjectConfig.conn!=null){
		        		 labelTest.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLUE));
		        		 labelTest.setText("Test Connection Successful!");
		        		 ResultSet rs = WebProjectConfig.conn.getMetaData().getSchemas();
		        			while (rs.next()) {
		        		         comboSchema.add(rs.getString(1));
		        		      }
		        		      rs.close();
		        		 comboSchema.select(0);
		        		 WebProjectConfig.selectedSchema = comboSchema.getText();
		        		 comboSchema.addSelectionListener(new SelectionAdapter(){
									public void widgetSelected(SelectionEvent e){
		        					WebProjectConfig.selectedSchema = comboSchema.getText();
		        				}
		        			});
		        		 setPageComplete(true);		        		 
		        		 IWizardPage page = getNextPage();   
		        		 if (page.getControl() != null)   
		        		  page.dispose();
		        		 
		        	 }
		         }catch(Exception e){
		        	 labelTest.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
		        	 labelTest.setText("Test Connection Failure!");
		        	 e.printStackTrace();
		         }
		        }
		      });
		
		Iterator<String> it = dbSourceMap.keySet().iterator();
		while(it.hasNext()){
			combo.add(it.next());
		}
		combo.select(0);
		combo.addSelectionListener(new SelectionAdapter(){
			@Override
			public void widgetSelected(SelectionEvent e){
				WebProjectConfig.projectDBConfig = dbSourceMap.get(combo.getText());
				textDriver.setText(WebProjectConfig.projectDBConfig.driver);
				textUrl.setText(WebProjectConfig.projectDBConfig.url);
				textUsername.setText(WebProjectConfig.projectDBConfig.username);
				textPassword.setText(WebProjectConfig.projectDBConfig.password);
			}
		});
		
		
		textDriver.setText(WebProjectConfig.projectDBConfig.driver);
		textUrl.setText(WebProjectConfig.projectDBConfig.url);
		textUsername.setText(WebProjectConfig.projectDBConfig.username);
		textPassword.setText(WebProjectConfig.projectDBConfig.password);
		
		
		labelType.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		combo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		labelDriver.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		textDriver.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		labelUrl.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		textUrl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		labelUsername.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		textUsername.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		labelPassword.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		textPassword.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		buttonTestConnection.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		labelTest.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		labelSchema.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		comboSchema.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
	}

	
	
}
